<template>
  <div class="user">
    <el-card shadow="always">
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        :inline="false"
        size="mini"
      >
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="昵称">
              <el-input v-model="form.nickName" placeholder="昵称" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="用户名">
              <el-input v-model="form.username" placeholder="用户名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="6" :offset="18">
            <el-button size="mini" @click="onReset()">重置</el-button>
            <el-button
              type="primary"
              size="mini"
              @click="onSearch"
            >搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="always">
      <div slot="header">
        <el-button
          type="primary"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </div>
      <el-table :data="list" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="昵称" prop="nick_name" />
        <el-table-column label="salt">
          <template slot-scope="scope">
            <div>
              <img :src="scope.row.salt" alt="" srcset="" class="avatar">
            </div>
          </template>
        </el-table-column>
        <el-table-column label="角色" prop="roles" />
        <el-table-column label="添加时间" prop="create_time" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div>
              <el-button
                type="success"
                size="mini"
                @click="handleEdit(scope.row)"
              >编辑</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="handleDel(scope.row)"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="footer">
        <el-pagination
          :current-page.sync="page.start"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.totalNum"
          @size-change="sizeChange"
          @current-change="currentChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script>
import { findUsersByPage, removeUser } from '@/api/auth/user/index'
export default {
  data() {
    return {
      form: {
        nickName: '',
        username: ''
      },
      list: [], // 用户列表
      page: {
        // 分页
        start: 1,
        limit: 10,
        totalNum: 0
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    // 获取用户列表
    async getUserList() {
      const { success, data, message } = await findUsersByPage(
        this.page.start,
        this.page.limit
      )
      if (success) {
        this.list = data.rows
        this.page.totalNum = data.total
      } else {
        this.$message.error(message)
      }
    },
    onSearch() {
      this.getUserList()
    },
    // 重置
    onReset() {
      this.form = undefined
    },
    // 新增
    handleAdd() {},
    // 编辑
    handleEdit() {},
    // 删除
    handleDel(item) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { success, message } = await removeUser(item.id)
          if (success) {
            this.$message.success('删除成功')
            // 重新获取用户
            this.getUserList()
          } else {
            this.$message.error(message)
          }
        })
        .catch(() => {
          this.$message.info('您取消了操作')
        })
    },
    sizeChange(newVal) {
      this.page.limit = newVal
      this.page.start = 1
      this.getUserList()
    },
    currentChange(newVal) {
      this.page.start = newVal
      this.getUserList()
    }
  }
}
</script>
<style lang="scss" scoped>
.user {
  margin: 30px;
  .avatar {
    width: 120px;
    height: 80px;
  }
  .footer {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      margin-top: 10px;
  }
}
</style>
